package cn.afterturn.svn.analysis.service.userinfo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Criteria;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.service.IdEntityService;

import cn.afterturn.svn.analysis.entity.userinfo.UserInfoEntity;

/**
 * svn用户服务
 * @author JueYue
 * @date 2015-07-29 15:41
 * @version V1.0  
 **/
@IocBean(name = "userInfoService", fields = { "dao" }, create = "init")
public class UserInfoService extends IdEntityService<UserInfoEntity> {

    private static final Log log = Logs.get();

    private static Map<String, UserInfoEntity> userMap;

    @SuppressWarnings("unchecked")
    public void init() {
        List<UserInfoEntity> list = dao().query(UserInfoEntity.class, null);
        userMap = (Map<String, UserInfoEntity>) Lang.collection2map(HashMap.class, list, "user");
    }

    public UserInfoEntity checkUser(String userName, Date updateTime) {
        if (!userMap.containsKey(userName)) {
            UserInfoEntity entity = new UserInfoEntity();
            entity.setLastTime(updateTime);
            entity.setUser(userName);
            dao().insert(entity);
            userMap.put(userName, entity);
        }
        return userMap.get(userName);
    }

    public QueryResult getUserInfoPage(UserInfoEntity entity, Pager pager) {
        Criteria cri = Cnd.cri();
        if (Strings.isNotBlank(entity.getName())) {
            cri.where().and("name", "=", entity.getName());
        }
        if (Strings.isNotBlank(entity.getUser())) {
            cri.where().and("user", "=", entity.getUser());
        }
        List<UserInfoEntity> list = dao().query(UserInfoEntity.class, cri, pager);
        pager.setRecordCount(dao().count(UserInfoEntity.class, cri));
        return new QueryResult(list, pager);
    }

}
